{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T21:58:56.725542Z",
     "start_time": "2025-07-04T21:58:55.119601Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导入必要的库\n",
    "import torch\n",
    "from tqdm import tqdm\n",
    "from modelscope import AutoTokenizer, AutoModel\n",
    "from peft import PeftModel, PeftConfig"
   ],
   "id": "8e3df76a45776c9a",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T21:58:58.073064Z",
     "start_time": "2025-07-04T21:58:57.699825Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 定义模型和预训练模型的路径\n",
    "model_dir = \"C:\\\\Users\\\\16014\\\\.cache\\\\modelscope\\\\hub\\\\models\\\\ZhipuAI\\\\chatglm3-6b\"\n",
    "peft_model_id = \"./lora_saver/lora_query_key_value\""
   ],
   "id": "6d0fe98be9960476",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T21:59:09.189004Z",
     "start_time": "2025-07-04T21:59:00.277452Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在不计算梯度的情况下进行模型加载和预处理\n",
    "with torch.no_grad():\n",
    "    tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)\n",
    "    model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()\n",
    "\n",
    "model = PeftModel.from_pretrained(model, peft_model_id)\n",
    "model.eval()"
   ],
   "id": "d734d6aa08425aa6",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/7 [00:00<?, ?it/s]"
      ],
      "application/vnd.jupyter.widget-view+json": {
       "version_major": 2,
       "version_minor": 0,
       "model_id": "c08a1c516b36411fab2223006df51f96"
      }
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "PeftModelForCausalLM(\n",
       "  (base_model): LoraModel(\n",
       "    (model): ChatGLMForConditionalGeneration(\n",
       "      (transformer): ChatGLMModel(\n",
       "        (embedding): Embedding(\n",
       "          (word_embeddings): Embedding(65024, 4096)\n",
       "        )\n",
       "        (rotary_pos_emb): RotaryEmbedding()\n",
       "        (encoder): GLMTransformer(\n",
       "          (layers): ModuleList(\n",
       "            (0-27): 28 x GLMBlock(\n",
       "              (input_layernorm): RMSNorm()\n",
       "              (self_attention): SelfAttention(\n",
       "                (query_key_value): lora.Linear(\n",
       "                  (base_layer): Linear(in_features=4096, out_features=4608, bias=True)\n",
       "                  (lora_dropout): ModuleDict(\n",
       "                    (default): Dropout(p=0.05, inplace=False)\n",
       "                  )\n",
       "                  (lora_A): ModuleDict(\n",
       "                    (default): Linear(in_features=4096, out_features=8, bias=False)\n",
       "                  )\n",
       "                  (lora_B): ModuleDict(\n",
       "                    (default): Linear(in_features=8, out_features=4608, bias=False)\n",
       "                  )\n",
       "                  (lora_embedding_A): ParameterDict()\n",
       "                  (lora_embedding_B): ParameterDict()\n",
       "                  (lora_magnitude_vector): ModuleDict()\n",
       "                )\n",
       "                (core_attention): CoreAttention(\n",
       "                  (attention_dropout): Dropout(p=0.0, inplace=False)\n",
       "                )\n",
       "                (dense): Linear(in_features=4096, out_features=4096, bias=False)\n",
       "              )\n",
       "              (post_attention_layernorm): RMSNorm()\n",
       "              (mlp): MLP(\n",
       "                (dense_h_to_4h): Linear(in_features=4096, out_features=27392, bias=False)\n",
       "                (dense_4h_to_h): Linear(in_features=13696, out_features=4096, bias=False)\n",
       "              )\n",
       "            )\n",
       "          )\n",
       "          (final_layernorm): RMSNorm()\n",
       "        )\n",
       "        (output_layer): Linear(in_features=4096, out_features=65024, bias=False)\n",
       "      )\n",
       "    )\n",
       "  )\n",
       ")"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T22:02:00.528261Z",
     "start_time": "2025-07-04T22:01:19.318438Z"
    }
   },
   "cell_type": "code",
   "source": [
    "history = []\n",
    "role = \"user\"\n",
    "while True:\n",
    "    # 通过键盘接收用户输入\n",
    "    query = input(\"请输入您的问题：\")\n",
    "\n",
    "    # 判断用户是否想退出对话\n",
    "    if query.lower() == \"exit\":\n",
    "        break\n",
    "\n",
    "        # 使用分词器构建聊天输入\n",
    "    inputs = tokenizer.build_chat_input(query, history=history, role=role)\n",
    "    inputs = inputs.to('cuda')\n",
    "\n",
    "    # 定义结束标记和生成参数\n",
    "    eos_token_id = [tokenizer.eos_token_id, tokenizer.get_command(\"<|user|>\"), tokenizer.get_command(\"<|observation|>\")]\n",
    "    gen_kwargs = {\"max_length\": 1200, \"num_beams\": 1, \"do_sample\": True, \"top_p\": 0.8, \"temperature\": 0.8}\n",
    "\n",
    "    # 生成输出\n",
    "    outputs = model.generate(**inputs, **gen_kwargs, eos_token_id=eos_token_id)\n",
    "    outputs = outputs.tolist()[0][len(inputs[\"input_ids\"][0]):-1]\n",
    "\n",
    "    # 使用分词器解码输出\n",
    "    response = tokenizer.decode(outputs)\n",
    "\n",
    "    # 处理响应，包括去除一些特殊标记和更新对话历史\n",
    "    response, history = model.process_response(response, history)\n",
    "\n",
    "    # 打印响应\n",
    "    print(\"模型响应：\", response)"
   ],
   "id": "e4b1b00d76d666b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型响应： 提高高三学生的成绩需要多方面的努力和措施,以下是一些建议:\n",
      "1. 制定合理的学习计划:让学生明确自己的学习目标,合理分配时间,制定可行的学习计划,并严格执行。\n",
      "2. 加强基础知识的学习:高三学生需要掌握扎实的基础知识,才能更好地理解和掌握更高难度的知识。\n",
      "3. 注重解题能力的培养:高三学生需要具备良好的解题能力,可以通过多做练习、参加模拟考试等方式来培养。\n",
      "4. 培养良好的学习习惯:良好的学习习惯是提高学习成绩的关键,包括注意力和学习态度等,可以通过培养学生的自律性、减少干扰等方式来实现。\n",
      "5. 提供有效的学习资源:为学生提供丰富、有效的学习资源,包括教学视频、课件、习题集等,帮助学生更好地理解和掌握知识。\n",
      "6. 加强师生互动:教师可以加强对学生的指导和鼓励,与学生进行更多的交流和互动,让学生感受到老师的关注和支持,从而提高学生的学习积极性和动力。\n",
      "7. 减轻学生心理压力:高三学习压力很大,需要教师及时关注学生的心理健康,为学生提供心理支持和帮助,减轻学生的心理压力,保持良好的心态。\n",
      "模型响应： 很高兴能为您提供帮助!欢迎你随时向我提问哦!\n",
      "模型响应： 看到你笑所以我也很开心呢!欢迎你继续向我提问或者和我聊天哦!\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-07-04T22:01:08.527256Z",
     "start_time": "2025-07-04T22:01:08.525240Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "ea059543bef21733",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "e8917adc1df7a03d"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
